package com.diligent.cloud.sfc.resources.impl;

import com.canny.commons.model.Codes;
import com.canny.commons.model.ListResult;
import com.canny.commons.model.ObjectResult;
import com.canny.commons.model.Result;
import com.diligent.cloud.sfc.resources.CraftDataResource;
import com.diligent.cloud.sfc.service.CraftDataService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RestController;

import java.security.Principal;

/**
 * CraftDataResourceImpl class
 *
 * @author xf107278
 * @date 3/28/2020
 */
@RestController
public class CraftDataResourceImpl implements CraftDataResource {
    private CraftDataService craftDataService;

    public CraftDataResourceImpl(CraftDataService craftDataService) {
        this.craftDataService = craftDataService;
    }

    @Override
    public ResponseEntity<Result> getElineRuntimeParams(String machCode, String deviceType) {
        try {
            return craftDataService.getElineRuntimeParams(machCode, deviceType);
        } catch (Exception e) {
            e.printStackTrace();
        }
        ObjectResult result = new ObjectResult();
        result.modifyCode(Codes.fail.getCode(),Codes.fail.getMsg());
        return ResponseEntity.ok(result);
    }

    @Override
    public ResponseEntity<ListResult> getElineEventTable(String machCode, String timeEnd, String duration) {
        try {
            return craftDataService.getElineEventTable(machCode, timeEnd, duration);
        } catch (Exception e) {
            e.printStackTrace();
        }
        ListResult result = new ListResult();
        result.modifyCode(Codes.fail.getCode(),Codes.fail.getMsg());
        return ResponseEntity.ok(result);
    }

    @Override
    public ResponseEntity<ObjectResult> getCraftDataResource(String machCode, Principal principal) {
        return craftDataService.getCraftDataResource(machCode, principal);
    }

    @Override
    public ResponseEntity<ObjectResult<String>> getItemIdByMatCode(String matCode) {
        return craftDataService.getItemIdByMatCode(matCode);
    }
}
